//
//  MQWYCell.m
//  MQ
//
//  Created by lanou on 15/10/26.
//  Copyright © 2015年 徐永祥. All rights reserved.
//

#import "MQWYCell.h"

@implementation MQWYCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _backImageView = [[UIImageView alloc] init];
        _backImageView.frame = CGRectMake(10, 10, SCREEN_WIDTH - 20, 200);
        _backImageView.userInteractionEnabled = YES;
        _backImageView.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:_backImageView];
        
        _contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, SCREEN_WIDTH - 40, 100)];
        _contentLabel.numberOfLines = 0;
        _contentLabel.font = [UIFont systemFontOfSize:18];
        _contentLabel.alpha = 0.8;
        [_backImageView addSubview:_contentLabel];
        
        _lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 120, SCREEN_WIDTH - 40, 2)];
        _lineLabel.backgroundColor = [UIColor grayColor];
        _lineLabel.alpha = 0.2;
        [_backImageView addSubview:_lineLabel];
        
        _upBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _upBtn.alpha = 0.8;
        _upBtn.frame = CGRectMake(20, 120, 30, 30);
        [_upBtn setImage:[UIImage imageNamed:@"unzan"] forState:(UIControlStateNormal)];
        [_backImageView addSubview:_upBtn];
        
        _upTimesLabel = [[UILabel alloc] initWithFrame:CGRectMake(55, 120, 50, 30)];
        _upTimesLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0.5 alpha:1];
        _upTimesLabel.alpha = 0.8;
        [_backImageView addSubview:_upTimesLabel];
        
        _likeBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _likeBtn.frame = CGRectMake((SCREEN_WIDTH - 30)/ 2, 120, 30, 30);
        _likeBtn.alpha = 0.8;
        [_likeBtn setImage:[UIImage imageNamed:@"star_unfav@2x"] forState:(UIControlStateNormal)];
        [_backImageView addSubview:_likeBtn];
        
        _shareBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _shareBtn.frame = CGRectMake(SCREEN_WIDTH - 60 , 120, 30, 30);
        _shareBtn.alpha = 0.8;
        [_shareBtn setImage:[UIImage imageNamed:@"fenxiang"] forState:(UIControlStateNormal)];
        [_backImageView addSubview:_shareBtn];
        
    }
    return self;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)configureWithModel:(MQWYModel *)model{
    CGFloat height = [MQWYCell heightForString:model.digest];
    _contentLabel.text = model.digest;
    _upTimesLabel.text = [NSString stringWithFormat:@"%ld", model.upTimes];
    
    _backImageView.frame = CGRectMake(10, 10, SCREEN_WIDTH - 20, height + 45);
    
    
    _contentLabel.frame = CGRectMake(10, 5, SCREEN_WIDTH - 40, height);
   
    _upBtn.frame = CGRectMake(20, height + 15, 20, 20);
    _upTimesLabel.frame = CGRectMake(43, height + 17, 80, 20);
    _likeBtn.frame = CGRectMake((SCREEN_WIDTH - 30)/ 2, height + 15, 20, 20);
    _shareBtn.frame = CGRectMake(SCREEN_WIDTH - 60 , height + 15, 20, 20);
     _lineLabel.frame = CGRectMake(0, height + 45, SCREEN_WIDTH , 2);
    
}
+ (CGFloat)heightForString:(NSString *)str{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:18] forKey:NSFontAttributeName];
    CGRect bounds = [str boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 40, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
    return bounds.size.height;
}
+ (CGFloat)heightForCellWithModel:(MQWYModel *)model{
    return [self heightForString:model.digest] + 60;
}

@end
